﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using TestSystem.BLL.TestSystem;

public partial class QuizResultItem : System.Web.UI.UserControl
{
    private int _testID;
    public int TestID
    {
        get { return _testID; }
        set{ _testID = value;}
    }

    private Test _test = null;
    public Test Test
    {
        get
        {
            if (_test == null)
                _test = ((List<Test>) Session["Tests"]).Find(test => test.TestID == TestID);
            return _test;
        }
    }

    private List<UserAnswer> _userAnswers = null;
    public List<UserAnswer> UserAnswers
    {
        get
        {
            if (_userAnswers == null)
            {
                var dictionary = (Dictionary<int, List<UserAnswer>>) Session["UserAnswers"];
                if(dictionary.ContainsKey(TestID))
                    _userAnswers = dictionary[TestID];
            }
            return _userAnswers;
        }
    }

    private List<Answer> _answers = null;
    public List<Answer> Answers
    {
        get
        {
            if (_answers == null)
                _answers = Test.Answers;
            return _answers;
        }
    }

    private List<Answer> _correctAnswers = null;
    public List<Answer> CorrectAnswers
    {
        get
        {
            if (_correctAnswers == null)
                _correctAnswers = Test.CorrectAnswers;
            return _correctAnswers;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && UserAnswers != null)
        {
            test.Attributes["class"] = IsCorrect() ? "question correct" : "question incorrect";
            questionText.InnerHtml = Test.Body + " (" + Test.Complexity + " " + GetNumberDeclension(Test.Complexity) + ")";
            switch (Test.TestType)
            {
                case 1:
                    answers.InnerHtml = GetInputRadios(true);
                    break;
                case 2:
                    answers.InnerHtml = GetInputRadios(false);
                    break;
                case 3:
                    answers.InnerHtml = GetInputText();
                    break;
                case 4:
                    goto case 3;
            }
        }
    }

    private bool IsCorrect()
    {
        if (UserAnswers.Count != CorrectAnswers.Count)
            return false;
        foreach (var userAnswer in UserAnswers)
        {
            if (CorrectAnswers.Find(answer => answer.AnswerID == userAnswer.UserAnswerID) == null)
                return false;
        }
        return true;
    }

    private string GetInputRadios(bool isRadio)
    {
        var radio = new StringBuilder();
        var type = isRadio ? "type='radio' " : "type='checkbox' ";
        foreach (var answer in Answers)
        {
            radio.Append("<li>");
            radio.Append("<input disabled='disabled' ");
            radio.Append(type);
            if (UserAnswers.Find(ans => ans.UserAnswerID == answer.AnswerID) != null)
                radio.Append("checked='checked'/>");
            radio.Append("<p class=");
            radio.Append(answer.IsCorrect ? "' correct '" : "''");
            radio.Append(">" + answer.Body + "</p>");
            radio.Append("</li>");
        }
        return radio.ToString();
    }

    private string GetInputText()
    {
        var text = new StringBuilder();
        text.Append("<div class='text-answer'><p>");
        text.Append("Ваша відповідь: <input type='text' readonly='readonly' value='");
        text.Append(UserAnswers[0].UserAnswerBody + "' </p>");
        text.Append("<p>Правильні відповіді:&nbsp;");
        foreach (var answer in CorrectAnswers)
        {
            text.Append(answer.Body + "&nbsp;");
        }
        text.Append("</p></div>");
        return text.ToString();
    }

    private string GetNumberDeclension(int number)
    {
        int tmp = number%10;
        switch (tmp)
        {
            case 1:
                return "бал";
                case 2:
            case 3:
            case 4:
                return "бали";
                case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                return "балів";
        }
        return "";
    }
}
